//
//  NewFeatureView.swift
//  MyLottery
//
//  Created by wujunjie on 2018/8/11.
//  Copyright © 2018年 wujunjie. All rights reserved.
// 新特性页

import UIKit

class NewFeatureView: UIView {
    lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView(frame: kScreenBounds)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.delegate = self
        scrollView.bounces = false
        return scrollView
    }()
    
    lazy var pageControl : UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor.red
        pageControl.pageIndicatorTintColor = UIColor.black
        pageControl.currentPage = 0
        pageControl.numberOfPages = 4
        pageControl.isUserInteractionEnabled = false
        return pageControl
    }()
    
     init() {
        super.init(frame:kScreenBounds)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension NewFeatureView {
    private func setupUI() {
        backgroundColor = UIColor.red
        addSubview(scrollView)
        addSubview(pageControl)
        for i in 1...4 {
            let imageView = UIImageView(image: UIImage(named: "\new_feature_(i)"))
            imageView.frame = CGRect(origin: CGPoint(x: kScreenWidth*CGFloat(i-1), y: 0), size: CGSize(width: kScreenWidth, height: kScreenHeight))
            print(imageView.frame)
            scrollView.addSubview(imageView)
        }
        scrollView.contentSize = CGSize(width:kScreenWidth*5, height:0)
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(-100)
        }
    }
}

extension NewFeatureView : UIScrollViewDelegate{
    func scrollViewDidScroll(scrollViewa: UIScrollView) {
        //scrollview滑动到屏幕的中间位置, 更改 pageControl的值
        let offsetx = scrollViewa.contentOffset.x
        //四舍五入,要记住 Int(小数 + 0.5)
        //分页算法 (总数 - 1) / 每页显示几个 + 1
        //更改pageControl的值
        let currentPage = Int (offsetx / kScreenWidth + 0.5)
        pageControl.currentPage = currentPage
        
        //当所有的imageView都显示完了就隐藏pageControl
        pageControl.isHidden = offsetx > (kScreenWidth * 3 + 100)
        
        //释放新功能页
        if offsetx >= kScreenWidth * 4 {
            self.removeFromSuperview()
        }
    }
}

